<?php
	class Account_model extends CI_Model {
		public function __construct() {
			parent::__construct();
			$this->load->library('encrypt');
		}
		
		public function getAccountStats($accountId) {
			$this->db->select('*');
			$this->db->from('users');
			$this->db->where('ID', $accountId);
			$result = $this->db->get();
			$result = $result->result_array();
			$account = $result[0];
			
			return $account;
		}
		
		public function verifyPassword($password, $accountId) {
			$this->db->select('password');
			$this->db->from('users');
			$this->db->where('ID', $accountId);
			$validateCombination = $this->db->get();
			$dbPassword = $validateCombination->result_array();
			$dbPassword = $this->encrypt->decode($dbPassword[0]['password']);
			
			return $dbPassword == $password;
		}
		
		public function verifyName($name) {
			$query = $this->db->query('SELECT count(*) as count FROM characters WHERE name = \'' . $name . '\'');
			$result = $query->result_array();
			
			return $result[0]['count'] == 0;
		}
		
		public function updateUserPassword($password, $accountId) {
			$passwordEncrypted = $this->encrypt->encode($password);
			$this->db->query('UPDATE users SET password = \'' . $passwordEncrypted . '\'');
		}
		
		public function createCharacter($name, $class, $accountId) {
			$this->db->query('INSERT INTO characters(user_id,name,class) VALUES(' . $accountId . ', \'' . $name . '\', \'' . $class . '\')');
			$charId = $this->db->insert_id();
			
			switch($class) {
				case 'Fighter':
					$this->db->query('UPDATE characters SET strength = 28 WHERE ID = ' . $charId);
					$this->db->query('UPDATE characters SET agility = 16 WHERE ID = ' . $charId);
					$this->db->query('UPDATE characters SET vitality = 23 WHERE ID = ' . $charId);
					$this->db->query('UPDATE characters SET spirit = 19 WHERE ID = ' . $charId);
					$this->db->query('UPDATE characters SET energy = 19 WHERE ID = ' . $charId);
					$this->db->query('UPDATE characters SET armor = 15 WHERE ID = ' . $charId);
					$this->db->query('UPDATE characters SET minattack = 3 WHERE ID = ' . $charId);
					$this->db->query('UPDATE characters SET maxattack = 5 WHERE ID = ' . $charId);
					$this->db->query('UPDATE characters SET curlife = 70 WHERE ID = ' . $charId);
					$this->db->query('UPDATE characters SET maxlife = 70 WHERE ID = ' . $charId);
					$this->db->query('UPDATE characters SET curmana = 0 WHERE ID = ' . $charId);
					$this->db->query('UPDATE characters SET maxmana = 100 WHERE ID = ' . $charId);
					$this->db->query('UPDATE characters SET level = 1 WHERE ID = ' . $charId);
					$this->db->query('UPDATE characters SET gold = 0 WHERE ID = ' . $charId);
					$this->db->query('UPDATE characters SET image = \'fighter.jpg\' WHERE ID = ' . $charId);
					$this->db->query('INSERT INTO character_equipment(char_id, slot, item_id) VALUES(' . $charId . ', \'head\', 0)');
					$this->db->query('INSERT INTO character_equipment(char_id, slot, item_id) VALUES(' . $charId . ', \'chest\', 9)');
					$this->db->query('INSERT INTO character_equipment(char_id, slot, item_id) VALUES(' . $charId . ', \'legs\', 12)');
					$this->db->query('INSERT INTO character_equipment(char_id, slot, item_id) VALUES(' . $charId . ', \'hands\', 11)');
					$this->db->query('INSERT INTO character_equipment(char_id, slot, item_id) VALUES(' . $charId . ', \'feet\', 10)');
					$this->db->query('INSERT INTO character_equipment(char_id, slot, item_id) VALUES(' . $charId . ', \'ear1\', 0)');
					$this->db->query('INSERT INTO character_equipment(char_id, slot, item_id) VALUES(' . $charId . ', \'ear2\', 0)');
					$this->db->query('INSERT INTO character_equipment(char_id, slot, item_id) VALUES(' . $charId . ', \'finger1\' 0)');
					$this->db->query('INSERT INTO character_equipment(char_id, slot, item_id) VALUES(' . $charId . ', \'finger2\', 0)');
					$this->db->query('INSERT INTO character_equipment(char_id, slot, item_id) VALUES(' . $charId . ', \'neck\', 0)');
					$this->db->query('INSERT INTO character_equipment(char_id, slot, item_id) VALUES(' . $charId . ', \'phand\', 1)');
					$this->db->query('INSERT INTO character_equipment(char_id, slot, item_id) VALUES(' . $charId . ', \'shand\', 0)');
					break;
				case 'Sorcerer':
					$this->db->query('UPDATE characters SET strength = 15 WHERE ID = ' . $charId);
					$this->db->query('UPDATE characters SET agility = 22 WHERE ID = ' . $charId);
					$this->db->query('UPDATE characters SET vitality = 20 WHERE ID = ' . $charId);
					$this->db->query('UPDATE characters SET spirit = 22 WHERE ID = ' . $charId);
					$this->db->query('UPDATE characters SET energy = 27 WHERE ID = ' . $charId);
					$this->db->query('UPDATE characters SET armor = 17 WHERE ID = ' . $charId);
					$this->db->query('UPDATE characters SET minattack = 2 WHERE ID = ' . $charId);
					$this->db->query('UPDATE characters SET maxattack = 4 WHERE ID = ' . $charId);
					$this->db->query('UPDATE characters SET curlife = 52 WHERE ID = ' . $charId);
					$this->db->query('UPDATE characters SET maxlife = 52 WHERE ID = ' . $charId);
					$this->db->query('UPDATE characters SET curmana = 225 WHERE ID = ' . $charId);
					$this->db->query('UPDATE characters SET maxmana = 225 WHERE ID = ' . $charId);
					$this->db->query('UPDATE characters SET level = 1 WHERE ID = ' . $charId);
					$this->db->query('UPDATE characters SET gold = 0 WHERE ID = ' . $charId);
					$this->db->query('UPDATE characters SET image = \'sorcerer.jpg\' WHERE ID = ' . $charId);
					$this->db->query('INSERT INTO character_equipment(char_id, slot, item_id) VALUES(' . $charId . ', \'head\', 0)');
					$this->db->query('INSERT INTO character_equipment(char_id, slot, item_id) VALUES(' . $charId . ', \'chest\', 9)');
					$this->db->query('INSERT INTO character_equipment(char_id, slot, item_id) VALUES(' . $charId . ', \'legs\', 12)');
					$this->db->query('INSERT INTO character_equipment(char_id, slot, item_id) VALUES(' . $charId . ', \'hands\', 11)');
					$this->db->query('INSERT INTO character_equipment(char_id, slot, item_id) VALUES(' . $charId . ', \'feet\', 10)');
					$this->db->query('INSERT INTO character_equipment(char_id, slot, item_id) VALUES(' . $charId . ', \'ear1\', 0)');
					$this->db->query('INSERT INTO character_equipment(char_id, slot, item_id) VALUES(' . $charId . ', \'ear2\', 0)');
					$this->db->query('INSERT INTO character_equipment(char_id, slot, item_id) VALUES(' . $charId . ', \'finger1\', 0)');
					$this->db->query('INSERT INTO character_equipment(char_id, slot, item_id) VALUES(' . $charId . ', \'finger2\', 0)');
					$this->db->query('INSERT INTO character_equipment(char_id, slot, item_id) VALUES(' . $charId . ', \'neck\', 0)');
					$this->db->query('INSERT INTO character_equipment(char_id, slot, item_id) VALUES(' . $charId . ', \'phand\', 1)');
					$this->db->query('INSERT INTO character_equipment(char_id, slot, item_id) VALUES(' . $charId . ', \'shand\', 0)');
					$this->db->query('INSERT INTO character_spells(char_id, spell_id) VALUES(' . $charId . ', 1)');
					break;
				case 'Rogue':
					$this->db->query('UPDATE characters SET strength = 17 WHERE ID = ' . $charId);
					$this->db->query('UPDATE characters SET agility = 27 WHERE ID = ' . $charId);
					$this->db->query('UPDATE characters SET vitality = 21 WHERE ID = ' . $charId);
					$this->db->query('UPDATE characters SET spirit = 20 WHERE ID = ' . $charId);
					$this->db->query('UPDATE characters SET energy = 20 WHERE ID = ' . $charId);
					$this->db->query('UPDATE characters SET armor = 27 WHERE ID = ' . $charId);
					$this->db->query('UPDATE characters SET minattack = 3 WHERE ID = ' . $charId);
					$this->db->query('UPDATE characters SET maxattack = 5 WHERE ID = ' . $charId);
					$this->db->query('UPDATE characters SET curlife = 55 WHERE ID = ' . $charId);
					$this->db->query('UPDATE characters SET maxlife = 55 WHERE ID = ' . $charId);
					$this->db->query('UPDATE characters SET curmana = 100 WHERE ID = ' . $charId);
					$this->db->query('UPDATE characters SET maxmana = 100 WHERE ID = ' . $charId);
					$this->db->query('UPDATE characters SET level = 1 WHERE ID = ' . $charId);
					$this->db->query('UPDATE characters SET gold = 0 WHERE ID = ' . $charId);
					$this->db->query('UPDATE characters SET image = \'rogue.jpg\' WHERE ID = ' . $charId);
					$this->db->query('INSERT INTO character_equipment(char_id, slot, item_id) VALUES(' . $charId . ', \'head\', 0)');
					$this->db->query('INSERT INTO character_equipment(char_id, slot, item_id) VALUES(' . $charId . ', \'chest\', 9)');
					$this->db->query('INSERT INTO character_equipment(char_id, slot, item_id) VALUES(' . $charId . ', \'legs\', 12)');
					$this->db->query('INSERT INTO character_equipment(char_id, slot, item_id) VALUES(' . $charId . ', \'hands\', 11)');
					$this->db->query('INSERT INTO character_equipment(char_id, slot, item_id) VALUES(' . $charId . ', \'feet\', 10)');
					$this->db->query('INSERT INTO character_equipment(char_id, slot, item_id) VALUES(' . $charId . ', \'ear1\', 0)');
					$this->db->query('INSERT INTO character_equipment(char_id, slot, item_id) VALUES(' . $charId . ', \'ear2\', 0)');
					$this->db->query('INSERT INTO character_equipment(char_id, slot, item_id) VALUES(' . $charId . ', \'finger1\', 0)');
					$this->db->query('INSERT INTO character_equipment(char_id, slot, item_id) VALUES(' . $charId . ', \'finger2\', 0)');
					$this->db->query('INSERT INTO character_equipment(char_id, slot, item_id) VALUES(' . $charId . ', \'neck\', 0)');
					$this->db->query('INSERT INTO character_equipment(char_id, slot, item_id) VALUES(' . $charId . ', \'phand\', 1)');
					$this->db->query('INSERT INTO character_equipment(char_id, slot, item_id) VALUES(' . $charId . ', \'shand\', 0)');
					break;
				case 'Monk':
					$this->db->query('UPDATE characters SET strength = 20 WHERE ID = ' . $charId);
					$this->db->query('UPDATE characters SET agility = 20 WHERE ID = ' . $charId);
					$this->db->query('UPDATE characters SET vitality = 20 WHERE ID = ' . $charId);
					$this->db->query('UPDATE characters SET spirit = 23 WHERE ID = ' . $charId);
					$this->db->query('UPDATE characters SET energy = 22 WHERE ID = ' . $charId);
					$this->db->query('UPDATE characters SET armor = 13 WHERE ID = ' . $charId);
					$this->db->query('UPDATE characters SET minattack = 2 WHERE ID = ' . $charId);
					$this->db->query('UPDATE characters SET maxattack = 4 WHERE ID = ' . $charId);
					$this->db->query('UPDATE characters SET curlife = 52 WHERE ID = ' . $charId);
					$this->db->query('UPDATE characters SET maxlife = 52 WHERE ID = ' . $charId);
					$this->db->query('UPDATE characters SET curmana = 160 WHERE ID = ' . $charId);
					$this->db->query('UPDATE characters SET maxmana = 160 WHERE ID = ' . $charId);
					$this->db->query('UPDATE characters SET level = 1 WHERE ID = ' . $charId);
					$this->db->query('UPDATE characters SET gold = 0 WHERE ID = ' . $charId);
					$this->db->query('UPDATE characters SET image = \'monk.jpg\' WHERE ID = ' . $charId);
					$this->db->query('INSERT INTO character_equipment(char_id, slot, item_id) VALUES(' . $charId . ', \'head\', 0)');
					$this->db->query('INSERT INTO character_equipment(char_id, slot, item_id) VALUES(' . $charId . ', \'chest\', 9)');
					$this->db->query('INSERT INTO character_equipment(char_id, slot, item_id) VALUES(' . $charId . ', \'legs\', 12)');
					$this->db->query('INSERT INTO character_equipment(char_id, slot, item_id) VALUES(' . $charId . ', \'hands\', 11)');
					$this->db->query('INSERT INTO character_equipment(char_id, slot, item_id) VALUES(' . $charId . ', \'feet\', 10)');
					$this->db->query('INSERT INTO character_equipment(char_id, slot, item_id) VALUES(' . $charId . ', \'ear1\', 0)');
					$this->db->query('INSERT INTO character_equipment(char_id, slot, item_id) VALUES(' . $charId . ', \'ear2\', 0)');
					$this->db->query('INSERT INTO character_equipment(char_id, slot, item_id) VALUES(' . $charId . ', \'finger1\', 0)');
					$this->db->query('INSERT INTO character_equipment(char_id, slot, item_id) VALUES(' . $charId . ', \'finger2\', 0)');
					$this->db->query('INSERT INTO character_equipment(char_id, slot, item_id) VALUES(' . $charId . ', \'neck\', 0)');
					$this->db->query('INSERT INTO character_equipment(char_id, slot, item_id) VALUES(' . $charId . ', \'phand\', 1)');
					$this->db->query('INSERT INTO character_equipment(char_id, slot, item_id) VALUES(' . $charId . ', \'shand\', 0)');
					break;
				case 'Warlock':
					$this->db->query('UPDATE characters SET strength = 15 WHERE ID = ' . $charId);
					$this->db->query('UPDATE characters SET agility = 22 WHERE ID = ' . $charId);
					$this->db->query('UPDATE characters SET vitality = 21 WHERE ID = ' . $charId);
					$this->db->query('UPDATE characters SET spirit = 22 WHERE ID = ' . $charId);
					$this->db->query('UPDATE characters SET energy = 26 WHERE ID = ' . $charId);
					$this->db->query('UPDATE characters SET armor = 17 WHERE ID = ' . $charId);
					$this->db->query('UPDATE characters SET minattack = 2 WHERE ID = ' . $charId);
					$this->db->query('UPDATE characters SET maxattack = 4 WHERE ID = ' . $charId);
					$this->db->query('UPDATE characters SET curlife = 53 WHERE ID = ' . $charId);
					$this->db->query('UPDATE characters SET maxlife = 53 WHERE ID = ' . $charId);
					$this->db->query('UPDATE characters SET curmana = 200 WHERE ID = ' . $charId);
					$this->db->query('UPDATE characters SET maxmana = 200 WHERE ID = ' . $charId);
					$this->db->query('UPDATE characters SET level = 1 WHERE ID = ' . $charId);
					$this->db->query('UPDATE characters SET gold = 0 WHERE ID = ' . $charId);
					$this->db->query('UPDATE characters SET image = \'warlock.jpg\' WHERE ID = ' . $charId);
					$this->db->query('INSERT INTO character_equipment(char_id, slot, item_id) VALUES(' . $charId . ', \'head\', 0)');
					$this->db->query('INSERT INTO character_equipment(char_id, slot, item_id) VALUES(' . $charId . ', \'chest\', 9)');
					$this->db->query('INSERT INTO character_equipment(char_id, slot, item_id) VALUES(' . $charId . ', \'legs\', 12)');
					$this->db->query('INSERT INTO character_equipment(char_id, slot, item_id) VALUES(' . $charId . ', \'hands\', 11)');
					$this->db->query('INSERT INTO character_equipment(char_id, slot, item_id) VALUES(' . $charId . ', \'feet\', 10)');
					$this->db->query('INSERT INTO character_equipment(char_id, slot, item_id) VALUES(' . $charId . ', \'ear1\', 0)');
					$this->db->query('INSERT INTO character_equipment(char_id, slot, item_id) VALUES(' . $charId . ', \'ear2\', 0)');
					$this->db->query('INSERT INTO character_equipment(char_id, slot, item_id) VALUES(' . $charId . ', \'finger1\', 0)');
					$this->db->query('INSERT INTO character_equipment(char_id, slot, item_id) VALUES(' . $charId . ', \'finger2\', 0)');
					$this->db->query('INSERT INTO character_equipment(char_id, slot, item_id) VALUES(' . $charId . ', \'neck\', 0)');
					$this->db->query('INSERT INTO character_equipment(char_id, slot, item_id) VALUES(' . $charId . ', \'phand\', 1)');
					$this->db->query('INSERT INTO character_equipment(char_id, slot, item_id) VALUES(' . $charId . ', \'shand\', 0)');
					break;
			}
		}
		
		public function updateUserCharacters($accountId) {
			$this->db->query('UPDATE users SET characters = characters + 1 WHERE ID = ' . $accountId);
		}
		
		public function getAccountCharacters($accountId) {
			$query = $this->db->query('SELECT * FROM characters WHERE user_id = ' . $accountId);
			$result = $query->result_array();
			
			return count($result) == 0 ? null : $result;
		}
		
		public function deleteCharacter($charId, $userId) {
			$this->db->query('DELETE FROM characters WHERE ID = ' . $charId);
			$this->db->query('DELETE FROM character_equipment WHERE char_id = ' . $charId);
			$this->db->query('DELETE FROM character_spells WHERE char_id = ' . $charId);
			$this->db->query('DELETE FROM character_inventory WHERE char_id = ' . $charId);
			$this->db->query('UPDATE users SET characters = characters - 1 WHERE ID = ' . $userId);
		}
		
		public function updateUserstats($info, $userId) {
			$this->db->query('UPDATE users SET fname = \'' . $info['fname'] . '\', lname = \'' . $info['lname'] . '\', nickname = \'' . $info['nickname'] . '\', country = \'' . $info['country'] . '\', city = \'' . $info['city'] . '\', birthdate = \'' . $info['birthdate'] . '\' WHERE ID = ' . $userId);
		}
	}
?>